package com.lzx.iteam;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.*;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
import android.text.ClipboardManager;
import android.view.*;
import android.widget.*;
import com.lzx.iteam.adapter.CloudContactAdapter;
import com.lzx.iteam.bean.*;
import com.lzx.iteam.contactssearch.conversion.Addressbook;
import com.lzx.iteam.contactssearch.conversion.PinyinSearch;
import com.lzx.iteam.engine.AyncGetContactInfo;
import com.lzx.iteam.net.*;
import com.lzx.iteam.provider.CloudDBOperation;
import com.lzx.iteam.util.*;
import com.umeng.analytics.MobclickAgent;
import io.rong.imkit.RongIM;

import java.util.*;

/**
 * Created by hfx on 15-1-12.
 */
public class ContactDetailActivity extends Activity implements View.OnClickListener {
    public static final String ACTION_CONTACT = "com.lzx.iteam.ACTION_CONTACT";

    private Context mContext;
    private Intent mIntent;
    private long mContactID;
    private ImageView mIvContactPhoto, mIvState;
    private TextView mTvContactName, mTvContactPosition, mTvContactCompany;
    private TextView mTvContactPhone,mTvContactEmail,mTvContactAddress,mTvContactNote;
    private RelativeLayout mTmMesssage;
    private LinearLayout mBtnChat, mContentLayout, mBtnBack, mEmailContent, mNoteContent, mWebsiteContent, mAddressContent;
    private LinearLayout mMore;
    private LinearLayout mLlDetatil;
    private LinearLayout mLlChat;
    private LinearLayout mLlMessage;
    private LayoutInflater mInflater;
    private PreferenceUtil mPreferenceUtil;
    private AyncGetContactInfo mGetContactInfo;
    private CloudDBOperation mCloudDBOperation;
    private ContactInfo mInfo;
    ImageView check_sc;
    ImageView check_dx;
    ImageView check_ip;

    private SendCardShakeListener mShaker;

    private AllDialogUtil mDialogUtil;

    //本地联系人相关
    private String mCallNumber;
    public boolean mIsContactModify = false;

    String userId = "", contactId = "", userPhone = "",userName="";

    private HashMap<String, String> mUsers;


    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {


            switch (msg.what) {
                case Constants.MSG_DATA_READY:
                    mInfo = (ContactInfo) msg.obj;
                    //setLocalContactData(mInfo);
                    setLocalData(mInfo);
                    break;


            }
        }
    };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_detail_layout);
        mContext = ContactDetailActivity.this;
        mIntent = getIntent();
        mContactID = mIntent.getLongExtra(Constants.INTENT_FLAG_CONTACTID, -1);
        initView();
        initProperty();
        mGetContactInfo.execute(new Long[]{mContactID});
        mUsers = (HashMap<String, String>) mPreferenceUtil.getCloudContact(PreferenceUtil.CLOUD_PHONE_2_USEID);
        if (mUsers == null) {
            mUsers = new HashMap<String, String>();
        }
        SealAppContext.getInstance().pushActivity(this);

    }

    private void initView() {
        mIvContactPhoto = (ImageView) findViewById(R.id.cloud_contact_photo);
        mIvState = (ImageView) findViewById(R.id.cloud_contact_status);
        mMore = (LinearLayout) findViewById(R.id.detail_ll_more);
        mTvContactName = (TextView) findViewById(R.id.cloud_contact_detail_name);
        mTvContactPosition = (TextView) findViewById(R.id.cloud_contact_detail_position);
        mTvContactCompany = (TextView) findViewById(R.id.cloud_contact_detail_company);
        mTvContactPhone = (TextView) findViewById(R.id.cloud_contact_detail_phone);
        mTvContactAddress = (TextView) findViewById(R.id.cloud_contact_detail_address);
        mTvContactEmail = (TextView) findViewById(R.id.cloud_contact_detail_email);
        mTvContactNote = (TextView) findViewById(R.id.contact_detail_note);
        mBtnChat = (LinearLayout) findViewById(R.id.ll_btn_chat);
        mBtnChat.setOnClickListener(this);
        mContentLayout = (LinearLayout) findViewById(R.id.views);
        mBtnBack = (LinearLayout) findViewById(R.id.btn_back);
        mBtnBack.setOnClickListener(this);
        mTmMesssage = (RelativeLayout) findViewById(R.id.contact_tm_message);
        mTmMesssage.setOnClickListener(this);

        mLlDetatil = (LinearLayout) findViewById(R.id.contact_detail_layout);
        mLlChat = (LinearLayout) findViewById(R.id.ll_btn_chat);
        mLlMessage = (LinearLayout) findViewById(R.id.ll_contact_detail_note);

    }


    private void initProperty() {
        mCloudDBOperation = new CloudDBOperation(ContactDetailActivity.this);
        mInflater = getLayoutInflater();
        mPreferenceUtil = PreferenceUtil.getInstance(mContext);
        mGetContactInfo = new AyncGetContactInfo(this, mHandler.obtainMessage(Constants.MSG_DATA_READY));
        mDialogUtil = AllDialogUtil.getInstance(this);
    }


    private ImageView mIcon;
    private TextView mTvIcon;
    private RelativeLayout mContainerIcon;


    /**
     * 长按电话号码复制时的确认弹窗
     *
     * @param phoneNumber
     */
    private void confirmCopy(final String phoneNumber) {
        new AlertDialog.Builder(mContext).setMessage("是否复制当前号码？")
                .setPositiveButton("复制", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ClipboardManager cm = (ClipboardManager) mContext
                                .getSystemService(Context.CLIPBOARD_SERVICE);
                        cm.setText(phoneNumber);
                    }
                }).setNegativeButton(R.string.cancel, null).show();
    }

    /**
     * 更新电话拨打方式
     *
     * @return
     */
    private int updataPhoneLabel() {
        int status = mPreferenceUtil.getInt(PreferenceUtil.CALL_STATUS, Constants.STATUS_DX_CALL);
        switch (status) {
            case Constants.STATUS_IP_CALL:
                mIcon.setImageResource(R.drawable.ip_icon_normal);
                mTvIcon.setText("IP拨打");
                break;
            case Constants.STATUS_DX_CALL:
                mIcon.setImageResource(R.drawable.fixed_line);
                mTvIcon.setText("固话拨打");
                break;
            case Constants.STATUS_SC_CALL:
                mIcon.setImageResource(R.drawable.second_call_icon);
                mTvIcon.setText("副号拨打");
                break;
        }
        mContainerIcon.setTag(status);
        return status;
    }

    private void updataCheck() {
        int status = mPreferenceUtil.getInt(PreferenceUtil.CALL_STATUS, Constants.STATUS_DX_CALL);
        switch (status) {
            case Constants.STATUS_IP_CALL:
                clearCheck();
                check_ip.setVisibility(View.VISIBLE);
                break;
            case Constants.STATUS_DX_CALL:
                clearCheck();
                check_dx.setVisibility(View.VISIBLE);
                break;
            case Constants.STATUS_SC_CALL:
                clearCheck();
                check_sc.setVisibility(View.VISIBLE);
                break;
        }
    }

    private void clearCheck() {
        check_dx.setVisibility(View.GONE);
        check_ip.setVisibility(View.GONE);
        check_sc.setVisibility(View.GONE);
    }

    /**
     * 新设置本地联系人
     * @param contactInfo
     */
    public void setLocalData(ContactInfo contactInfo){
        setDisplayName(contactInfo.getName());
        setContactPhoto(contactInfo.getPhoto());
        setCompanyAndPosition(contactInfo);
        setLocalPhone(contactInfo);
        setLocalEmail(contactInfo);
        setLocalAddress(contactInfo);
        setContactNote(contactInfo);
    }

    public void setLocalPhone(ContactInfo contactInfo){
        ArrayList<String> numbers = contactInfo.getArrayphoneNumbers();
        if (numbers != null && numbers.size()>0){
            mTvContactPhone.setText(numbers.get(0));
            mTvContactPhone.setTextColor(getResources().getColor(R.color.cloud_contact_detail_blue));
            mTvContactPhone.setOnClickListener(this);
        }
    }

    public void setLocalEmail(ContactInfo contactInfo){
        HashMap<String, String> emails = contactInfo.getEmails();
        if (emails != null && emails.size() > 0) {
            Iterator it = emails.keySet().iterator();
            if (it.hasNext()) {
                String value = (String) it.next();
                mTvContactEmail.setText(value);

            }
        }
    }

    private void setLocalAddress(ContactInfo contactInfo) {
        HashMap<String, String> addresses = contactInfo.getAddresses();
        if (addresses != null && addresses.size() > 0) {
            Iterator it = addresses.keySet().iterator();
            if (it.hasNext()) {
                String value = (String) it.next();
                mTvContactAddress.setText(value);
            }
        }
    }

    private void showCallDialog() {
        mDialogUtil.titleMsgBtnClick("",mTvContactPhone.getText().toString().trim(),"发短信","打电话");
        mDialogUtil.setOnDialogListener(new AllDialogUtil.DialogOnClickListener() {

            @Override
            public void onConfirmClick(Dialog lDialog) {
                CommonCodeUtil.launchSendMessage(ContactDetailActivity.this, mTvContactPhone.getText().toString().trim(), null);
                lDialog.dismiss();

            }

            @Override
            public void onCancelClick(Dialog lDialog) {
                CommonCodeUtil.launchCallByNumber(ContactDetailActivity.this, mTvContactPhone.getText().toString().trim());
                lDialog.dismiss();
            }
        });

    }


    /**
     * 原来设置本地联系人数据
     *
     * @param contactInfo
     */
    public void setLocalContactData(ContactInfo contactInfo) {
        updataLocalPhoneItem(contactInfo);
        updataEmailViews(contactInfo);
        updataWebViews(contactInfo);
        updataAddressViews(contactInfo);
        updataNoteViews(contactInfo);
        setTitleViews(contactInfo);
    }

    public void setTitleViews(ContactInfo contactInfo) {
        userName=contactInfo.getName();
        setDisplayName(contactInfo.getName());
        setCompanyAndPosition(contactInfo);
        setContactPhoto(contactInfo.getPhoto());
    }

    public void setDisplayName(String displayName) {
        if (StringUtil.isEmpty(displayName)) {
            displayName = "无名称";
        }
        mTvContactName.setText(displayName);
    }

    private void setCompanyAndPosition(ContactInfo info) {
        ArrayList<String> companys = info.getCompanys();
        ArrayList<String> positions = info.getPositions();
        if (companys != null && companys.size() > 0){
            mTvContactCompany.setText(companys.get(0));
        }
        if (positions != null && positions.size() > 0) {
            mTvContactPosition.setText(positions.get(0));
        }else {
            mTvContactPosition.setVisibility(View.GONE);

        }
    }

    private void setContactPhoto(Bitmap photo) {
        if (photo == null) {
            mIvContactPhoto.setImageResource(R.drawable.default_cloud_image);
        } else {
            photo = ImageUtil.createRoundBitmap(photo);
            mIvContactPhoto.setImageBitmap(photo);
        }
        //mIvContactPhoto.setEnabled(true);
    }

    private void setContactNote(ContactInfo contactInfo) {
        ArrayList<String> notes = contactInfo.getNotes();
        if (notes != null && notes.size() > 0) {
            mTvContactNote.setText(notes.get(0));
        }
    }

    private void getStatusAndText(long contactId) {
        if (!LocalLogin.getInstance().isLogedin()) return;

        @SuppressWarnings("unchecked")
        HashMap<Long, Integer> distStatus = (HashMap<Long, Integer>)
                PreferenceUtil.getInstance(mContext).getCloudContact(PreferenceUtil.CONTACT_ID_2_DIST_STATUS);
        @SuppressWarnings("unchecked")
        HashMap<Long, String> textStatus = (HashMap<Long, String>)
                PreferenceUtil.getInstance(mContext).getCloudContact(PreferenceUtil.CONTACT_ID_2_TEXT_STATUS);
        if (textStatus == null || distStatus == null) return;

    }

    private void updataLocalPhoneItem(ContactInfo contactInfo) {
        if (mContactID != -1) {
            mContentLayout = (LinearLayout) findViewById(R.id.views);
            mContentLayout.removeAllViews();
            Addressbook.Person person = mPreferenceUtil.getPerson(mContactID);
            ArrayList<String> numbers = new ArrayList<String>();
            HashMap<String, String> mapNumbers = contactInfo.getPhoneNumbers();
            if (person != null) {
                List<Addressbook.Person.PhoneNumber> phoneNumber = person.getPhoneList();
                if (phoneNumber != null && phoneNumber.size() > 0) {
                    for (int i = 0; i < phoneNumber.size(); i++) {
                        String number = phoneNumber.get(i).getNumber();
                        String label = mapNumbers.get(number);
                        numbers.add(number);
                        mContentLayout.addView(getLocalPhoneItemView(number, label));
//                        if (i + 1 != phoneNumber.size()) {
                        mContentLayout.addView(addLine(Constants.dip2px(this, 15)));
//                        }
                    }
                }
            }
        }
    }


    /**
     * 设置邮箱地址
     *
     * @param contactInfo
     */
    private void updataEmailViews(ContactInfo contactInfo) {
        mEmailContent = (LinearLayout) findViewById(R.id.views_email);
        mEmailContent.removeAllViews();
        HashMap<String, String> emails = contactInfo.getEmails();
        if (emails != null && emails.size() > 0) {
            Iterator it = emails.keySet().iterator();
            while (it.hasNext()) {
                String value = (String) it.next();
//                String label = emails.get(value);
                String label = "邮箱";
                mEmailContent.addView(otherItemViews(label, value, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE));
                mEmailContent.addView(addLine(Constants.dip2px(this, 15)));
            }
        }
    }


    /**
     * 设置地址
     *
     * @param contactInfo
     */
    private void updataAddressViews(ContactInfo contactInfo) {
        mAddressContent = (LinearLayout) findViewById(R.id.views_address);
        mAddressContent.removeAllViews();
        HashMap<String, String> addresses = contactInfo.getAddresses();
        if (addresses != null && addresses.size() > 0) {
            if (mWebsiteContent.getChildCount() > 0 || mEmailContent.getChildCount() > 0) {
//                mAddressContent.addView(addLine(Constants.dip2px(this, 15)));
            }
            Iterator it = addresses.keySet().iterator();
            while (it.hasNext()) {
                String value = (String) it.next();
                String label = addresses.get(value);
                mAddressContent.addView(otherItemViews(label, value, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE));
//                if (it.hasNext()) {
                mAddressContent.addView(addLine(Constants.dip2px(this, 15)));
//                }
            }
        }
    }

    /**
     * 设置主页
     *
     * @param contactInfo
     */
    private void updataWebViews(ContactInfo contactInfo) {
        mWebsiteContent = (LinearLayout) findViewById(R.id.views_website);
        mWebsiteContent.removeAllViews();
        ArrayList<String> websites = contactInfo.getWebsites();
        if (websites != null && websites.size() > 0) {
            if (mEmailContent.getChildCount() > 0) {
//                mWebsiteContent.addView(addLine(30));
            }
            for (int i = 0; i < websites.size(); i++) {
                String label = "主页";
                String value = websites.get(i);
                mWebsiteContent.addView(otherItemViews(label, value, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE));
//                if (i != websites.size() - 1) {
                mWebsiteContent.addView(addLine(Constants.dip2px(this, 15)));
//                }
            }
        }
    }

    /**
     * 设置备注
     *
     * @param contactInfo
     */
    private void updataNoteViews(ContactInfo contactInfo) {
        mNoteContent = (LinearLayout) findViewById(R.id.views_notes);
        mNoteContent.removeAllViews();
        ArrayList<String> notes = contactInfo.getNotes();
        if (notes != null && notes.size() > 0) {

            for (int i = 0; i < notes.size(); i++) {
                String value = notes.get(i);
                String label = "备注";
                mNoteContent.addView(otherItemViews(label, value, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE));
                mNoteContent.addView(addLine(Constants.dip2px(this, 15)));
            }
        }
    }

    /**
     * 动态加载一个信息item元素
     *
     * @param label
     * @param value
     * @param mimeType
     * @return
     */
    private View otherItemViews(String label, String value, String mimeType) {
        View item = mInflater.inflate(R.layout.view_contact_item, null);
        TextView labelText = setText(item, R.id.item_label, label);
        labelText.setTag(mimeType);
        setText(item, R.id.item_value, value);
        item.setOnClickListener(this);
        return item;
    }

    /**
     * 设置textview文字
     *
     * @param view
     * @param resourseId
     * @param charSequence
     * @return
     */
    private TextView setText(View view, int resourseId, CharSequence charSequence) {
        TextView textView = (TextView) view.findViewById(resourseId);
        if (textView != null) {
            textView.setText(charSequence);
        }
        return textView;
    }

    /**
     * 获取团队人单个号码的布局view
     *
     * @param phoneNumber
     * @param phoneType
     * @return
     */
    private View getLocalPhoneItemView(final String phoneNumber, String phoneType) {
        View item = mInflater.inflate(R.layout.display_phone_list, null);
        View phoneItem = item.findViewById(R.id.phone_item);
        View callItem = item.findViewById(R.id.detail_ll_call);
        TextView mPhoneNumView = (TextView) item.findViewById(R.id.phone_num);
        String number = phoneNumber;
        number = com.lzx.iteam.util.PhoneNumberUtils.cutPlus86Formatter(number);
        mCallNumber = phoneNumber;
        mCallNumber = com.lzx.iteam.util.PhoneNumberUtils.cutPlus86Formatter(mCallNumber);
        if (!StringUtil.isEmpty(mCallNumber) && mCallNumber.equals(number)) {
            mPhoneNumView.setTextColor(0xff000000);
        } else {
            mPhoneNumView.setTextColor(0xff000000);
        }
        ImageView mSmsView = (ImageView) item.findViewById(R.id.sms);
        TextView mPhoneArea = (TextView) item.findViewById(R.id.phone_label);// Label+Area
        mPhoneArea.setTextColor(0xff9d9d9d);
        mIcon = (ImageView) item.findViewById(R.id.iv_icon_view);
        mTvIcon = (TextView) item.findViewById(R.id.icon_text);
        mContainerIcon = (RelativeLayout) item.findViewById(R.id.container_icon);

        mSmsView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
//                getParent().setResult(Constants.ACTIVITY_RESULT_CLEAR_DIAL_NUM);
                CommonCodeUtil.launchSendMessage(ContactDetailActivity.this, phoneNumber, null);
                updataPerson(phoneNumber);
            }
        });

        phoneItem.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                confirmCopy(phoneNumber);

                return false;
            }
        });

        phoneItem.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
//                getParent().setResult(Constants.ACTIVITY_RESULT_CLEAR_DIAL_NUM);
                CommonCodeUtil.launchCallByNumber(ContactDetailActivity.this, phoneNumber);
                updataPerson(phoneNumber);
            }
        });

        callItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonCodeUtil.launchCallByNumber(ContactDetailActivity.this, phoneNumber);
                updataPerson(phoneNumber);
            }
        });

        mPhoneArea.setText(phoneType);
        mPhoneNumView.setText(phoneNumber);
        updataPhoneLabel();
        return item;
    }

    /**
     * 。。。什么鬼？暂时不知道干嘛用的，知道的改下注释
     *
     * @param num
     */
    private void updataPerson(String num) {
        Addressbook.Person person = mPreferenceUtil.getPerson(mContactID);
        if (person != null) {

            PinyinSearch.ContactInPersist memory = new PinyinSearch.ContactInPersist();
            memory.fuzzyString = person.getFuzzyString();

            List<Addressbook.Person.PhoneNumber> phoneNumber = person.getPhoneList();
            if (phoneNumber != null) {
                memory.phoneNumber = new ArrayList<String>();
                Iterator<Addressbook.Person.PhoneNumber> phoneNumberIt = phoneNumber.iterator();
                while (phoneNumberIt.hasNext()) {
                    Addressbook.Person.PhoneNumber phoneNumberElement = phoneNumberIt.next();
                    if (phoneNumberElement != null) {
                        memory.phoneNumber.add(phoneNumberElement.getNumber());
                    }
                }
            }

            if (memory.phoneNumber.remove(num)) {
                memory.phoneNumber.add(0, num);
            }

            List<Addressbook.Person.PhoneNumberArea> phoneNumberAreaList = person.getPhoneNumberAreaList();
            if (phoneNumberAreaList != null) {
                memory.phoneNumberArea = new ArrayList<String>();
                Iterator<Addressbook.Person.PhoneNumberArea> phoneNumberAreaIt = phoneNumberAreaList.iterator();
                while (phoneNumberAreaIt.hasNext()) {
                    Addressbook.Person.PhoneNumberArea phoneNumberAreaElement = phoneNumberAreaIt.next();
                    if (phoneNumberAreaElement != null) {
                        memory.phoneNumberArea.add(phoneNumberAreaElement.getArea());
                    }
                }
            }

            memory.organization = person.getOrganization();

            List<Addressbook.Person.GroupIdSet> groupIdSet = person.getGroupIdSetList();
            if (groupIdSet != null) {
                memory.groupIdSet = new HashSet<Long>();
                Iterator<Addressbook.Person.GroupIdSet> groupIdSetIt = groupIdSet.iterator();
                while (groupIdSetIt.hasNext()) {
                    Addressbook.Person.GroupIdSet groupId = groupIdSetIt.next();
                    if (groupId != null)
                        memory.groupIdSet.add(groupId.getGroupId());
                }
            }

            memory.email = person.getEmail();
            memory.fullName = person.getDisplayName();
            memory.photoId = person.getPhotoId();
            memory.rawId = person.getRawId();
            memory.version = person.getVersion();

            mPreferenceUtil.deletePerson(mContactID);
            mPreferenceUtil.savePerson(mContactID, memory);

        }
    }

    /**
     * 创建分割线view
     *
     * @return
     */
    private View addLine(int leftMargin) {
        View view = new View(this);
        view.setBackgroundColor(0xffcccccc);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, Constants.dip2px(this, 0.6f));
        layoutParams.setMargins(leftMargin, 0, 0, 0);
        view.setLayoutParams(layoutParams);
        return view;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_back://返回
                if (mLlMessage.getVisibility() == View.VISIBLE){
                    mLlMessage.setVisibility(View.GONE);
                    mLlDetatil.setVisibility(View.VISIBLE);
                    mLlChat.setVisibility(View.VISIBLE);
                    return;
                }
                finish();
                break;
            case R.id.ll_btn_chat:
                userPhone = mPreferenceUtil.getPerson(mContactID).getPhone(0).getNumber();
                userId = mUsers.get(PhoneNumberArea.formatPhoneNumDisplay(userPhone));
                contactId = "0";
                if (mPreferenceUtil.getString(PreferenceUtil.CLIENT_USEID, "0").equals(userId)) {
                    Toast.makeText(mContext, "对不起，您不能和自己聊天", Toast.LENGTH_LONG).show();
                } else {
                    if ("0".equals(userId) || StringUtil.isEmpty(userId)) {
                        Toast.makeText(mContext, "该用户尚未注册，请邀请他注册后再发聊天消息", Toast.LENGTH_LONG).show();
                    } else {
                        /*addChatGroupContact(userId, contactId);*/
                        RongIM.getInstance().startPrivateChat(ContactDetailActivity.this, userId, userName);

                    }
                }
                break;
            case R.id.contact_tm_message:
                mLlMessage.setVisibility(View.VISIBLE);
                mLlDetatil.setVisibility(View.GONE);
                mLlChat.setVisibility(View.GONE);
                break;
            case R.id.cloud_contact_detail_phone:
                showCallDialog();
                break;
        }
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (requestCode) {
            case Constants.ACTIVITY_REQUEST_EDITCONTACT:
                if (resultCode == RESULT_OK) {
                    mIsContactModify = true;
                    mGetContactInfo = new AyncGetContactInfo(this, mHandler.obtainMessage(Constants.MSG_DATA_READY));
                    mGetContactInfo.execute(new Long[]{mContactID});
                }
                break;
        }
    }

    @Override
    protected void onResume() {
        if (mShaker != null) {
            mShaker.resume();
        } else {
            mShaker = initShake();
        }
        MobclickAgent.onResume(this);
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
        if (mShaker != null)
            mShaker.pause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        CloudContactAdapter.opening = true;
        if (mShaker != null)
            mShaker.pause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mIsContactModify = false;
        if (mShaker != null) {
            mShaker.pause();
            mShaker = null;
        }
    }

    @Override
    public void onBackPressed() {
        if (mLlMessage.getVisibility() == View.VISIBLE){
            mLlMessage.setVisibility(View.GONE);
            mLlDetatil.setVisibility(View.VISIBLE);
            mLlChat.setVisibility(View.VISIBLE);
            return;
        }
        finish();
        super.onBackPressed();
    }

    public SendCardShakeListener initShake() {
        SendCardShakeListener shaker = new SendCardShakeListener(this);
        shaker.setOnShakeListener(new SendCardShakeListener.OnShakeListener() {

            public void onShake() {
                if (!StringUtil.isEmpty(mCallNumber)) {
                    CommonCodeUtil.launchCallByNumber(ContactDetailActivity.this, mCallNumber);

                }
            }
        });
        return shaker;
    }

}